<?php

class directorsActions extends sfActions {

    public function executeIndex(sfWebRequest $request){
        $c = new Criteria();
        $c->add(DirectorsPeer::ENABLED, 1);
        $c->addAscendingOrderByColumn(DirectorsPeer::ID);

        $pager = new sfPropelPager('Directors', sfConfig::get('app_max_per_page'));
        $pager->setCriteria($c);
        $pager->setPage($request->getParameter('page', 1));
        $pager->init();
        $this->pager = $pager;

        $x=new Criteria();
        $x->add(TextsPeer::ID,3);
        $periodo=TextsPeer::doSelectOne($x);

        $this->periodo=$periodo;
    }

    public function executeNew(sfWebRequest $request){

    }

   public function executePeriodo(sfWebRequest $request){

    }

   public function executePeriodo_save(sfWebRequest $request){
   
       $periodo=$request->getParameter('periodo');

       if($periodo=='')
         {
        $this->getUser()->setFlash('msg','No se admiten periodos vacios');
        $this->redirect('@directors_edit_periodo');
         }
       $c=new Criteria();
       $c->add(TextsPeer::ID,3);
       $tiempo=TextsPeer::doSelectOne($c);
       $tiempo->setContent($periodo);
       $tiempo->save();

        $this->getUser()->setFlash('msg','Periodo creado satisfactoriamente');
        $this->redirect('@directors');

    }




    public function executeNew_save(sfWebRequest $request){
        $name = $request->getParameter('name');
        $position = $request->getParameter('position');
        $fecha = $request->getParameter('fecha');
        $content = $request->getParameter('content');
        $email = $request->getParameter('email');

       $imagen = $_FILES["image"]["tmp_name"];

     

        $director = new Directors();
        $director->setName($name);
        $director->setPosition($position);
        $director->setContent($content);
        $director->setEmail($email);
        $director->setEnabled(1);
        $director->setFecha($fecha);
        $director->save();


       if($imagen!=''){
            $DS = DIRECTORY_SEPARATOR;
            move_uploaded_file($imagen, sfConfig::get('sf_upload_dir').$DS."autoridades".$DS.$director->getId().".jpg");
        }

        $this->getUser()->setFlash('msg','Director(a) creado(a) satisfactoriamente');
        $this->redirect('@directors');
    }

    public function executeEdit(sfWebRequest $request){
        $id = $request->getParameter('id');
        $this->director = DirectorsPeer::retrieveByPK($id);
    }

    public function executeEdit_save(sfWebRequest $request){
        $hdId = $request->getParameter('hdId');
        $name = $request->getParameter('name');
        $position = $request->getParameter('position');
        $fecha = $request->getParameter('fecha');
        $content = $request->getParameter('content');
        $email = $request->getParameter('email');

             $imagen = $_FILES["image"]["tmp_name"];


        $director = DirectorsPeer::retrieveByPK($hdId);
        $director->setName($name);
        $director->setPosition($position);
        $director->setContent($content);
        $director->setEmail($email);
        $director->setFecha($fecha);
        $director->save();


            if($imagen!=''){
            $DS = DIRECTORY_SEPARATOR;
            move_uploaded_file($imagen, sfConfig::get('sf_upload_dir').$DS."autoridades".$DS.$director->getId().".jpg");
        }

        $this->getUser()->setFlash('msg','Director(a) modificado(a) satisfactoriamente');
        $this->redirect('@directors');
    }

    public function executeDelete(sfWebRequest $request){
        $id = $request->getParameter('id');
        $director = DirectorsPeer::retrieveByPK($id);
        $director->setEnabled(0);
        $director->save();

        $this->getUser()->setFlash('msg','Director(a) eliminado(a) satisfactoriamente');
        $this->redirect('@directors');
    }

}